package testcase.app;

import common.Host;
import common.TestData;

import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import api.app.UpdatePassword;

/**
 * 帮忙医APP修改密码UpdatePassword接口测试用例
 * @author maojj
 *
 */
public class TestUpdatePassword
{
	/**
	 * 帮忙医APP修改密码UpdatePassword接口测试环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Test")
	public static String[][] testUpdatePasswordDataProvider() throws Exception
	{
		Host.setup("Test");
		TestData td = new TestData("app/TestUpdatePassword.xml");
		return td.getParam();	
	}
	
	/**
	 * 帮忙医APP修改密码UpdatePassword接口生产环境数据驱动
	 * @return 测试数据
	 * @throws Exception
	 */
	@DataProvider (name = "Prod")
	public static String[][] prodUpdatePasswordDataProvider() throws Exception
	{
		Host.setup("Prod");
		TestData td = new TestData("app/TestUpdatePassword.xml");
		return td.getParam();	
	}
	
	/**
	 * 帮忙医APP修改密码UpdatePassword接口测试环境用例
	 * @param caseName - 用例名称
	 * @param userAccount - 账号
	 * @param password - 新密码
	 * @param oldPassword - 原密码
	 * @throws Exception
	 */
	@Test (dataProvider = "Test", groups = {"Test"})
	public void testUpdatePasswordTestCase(String caseName, String userAccount, String password, String oldPassword) throws Exception
	{
		String result = UpdatePassword.post(userAccount, password, oldPassword);
		
		JSONObject jsonResult;
		if (result==null || result.isEmpty())
		{
			jsonResult = null;
		}
		else
		{
			jsonResult = new JSONObject(result);
		}

		
		//设置检查点
		Boolean checkpoint;
		switch (caseName) {
		case "UpdatePassword":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertTrue(checkpoint);	//接口返回Success字段是否为True
			break;
		case "OldpasswordError":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertFalse(checkpoint);	//接口返回Success字段是否为False
			break;
		case "UserAccountNull":
			checkpoint = result.isEmpty();
			Assert.assertTrue(checkpoint);	//接口返回是否为空
			break;
		case "PasswordNull":
			checkpoint = result.isEmpty();
			Assert.assertTrue(checkpoint);	//接口返回是否为空
			break;
		case "OldPasswordNull":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertFalse(checkpoint);	//接口返回Success字段是否为False
			break;
		default:
			break;
		}
	}
	
	/**
	 * 帮忙医APP修改密码UpdatePassword接口生产环境用例
	 * @param caseName - 用例名称
	 * @param userAccount - 账号
	 * @param password - 新密码
	 * @param oldPassword - 原密码
	 * @throws Exception
	 */
	@Test (dataProvider = "Prod", groups = {"Prod"})
	public void prodUpdatePasswordTestCase(String caseName, String userAccount, String password, String oldPassword) throws Exception
	{
		String result = UpdatePassword.post(userAccount, password, oldPassword); 
		
		JSONObject jsonResult;
		if (!result.isEmpty())
		{
			jsonResult = new JSONObject(result);
		}
		else
		{
			jsonResult = null;
		}

		//检查点：checkpoint
		Boolean checkpoint;
		switch (caseName) {
		case "UpdatePassword":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertTrue(checkpoint);	//接口返回Success字段是否为True
			break;
		case "OldpasswordError":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertFalse(checkpoint);	//接口返回Success字段是否为False
			break;
		case "UserAccountNull":
			checkpoint = result.isEmpty();
			Assert.assertTrue(checkpoint);	//接口返回是否为空
			break;
		case "PasswordNull":
			checkpoint = result.isEmpty();
			Assert.assertTrue(checkpoint);	//接口返回是否为空
			break;
		case "OldPasswordNull":
			checkpoint = jsonResult.getBoolean("Success");
			Assert.assertFalse(checkpoint);	//接口返回Success字段是否为False
			break;
		default:
			break;
		}

	}

}
